package com.bw.zg3park2212.adpter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bw.base.entity.NewsEntityItem
import com.bw.zg3park2212.R
import com.bw.zg3park2212.databinding.ItemNewsBinding
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.listener.OnItemLongClickListener

class NewsAdpter :PagingDataAdapter<NewsEntityItem,NewsAdpter.NewsViewHodel>(diff) {
    class NewsViewHodel(itemview:View):ViewHolder(itemview){
        val binding=DataBindingUtil.bind<ItemNewsBinding>(itemview)
    }
    companion object{
        val diff=object: DiffUtil.ItemCallback<NewsEntityItem>(){
            override fun areItemsTheSame(
                oldItem: NewsEntityItem,
                newItem: NewsEntityItem,
            ): Boolean {
                return oldItem==newItem
            }

            override fun areContentsTheSame(
                oldItem: NewsEntityItem,
                newItem: NewsEntityItem,
            ): Boolean {
                return oldItem.id==newItem.id
            }
        }
    }

    override fun onBindViewHolder(holder: NewsViewHodel, position: Int) {
        val item =getItem(position)!!
        holder.binding!!.newsTitle.setText(item!!.title)
        holder.binding!!.newsContent.setText(item!!.content)
        holder.binding!!.newsTime.setText(item!!.publishDate)
        holder.itemView.setOnClickListener {
            mOnItemClickListener?.onItemClick(item,position)
        }
        holder.itemView.setOnLongClickListener {
            mOnItemLongClickListener?.onItemLongClick(item,position)
            return@setOnLongClickListener true
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsViewHodel {
        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.item_news, null)
        return NewsViewHodel(inflate)
    }
    private var mOnItemClickListener: OnItemClickListener? = null
    private var mOnItemLongClickListener: OnItemLongClickListener? = null
    var data: MutableList<NewsEntityItem> = mutableListOf()
        internal set

     interface OnItemClickListener {
        fun onItemClick(item: NewsEntityItem, position: Int)
    }
     interface OnItemLongClickListener {
        fun onItemLongClick(item: NewsEntityItem, position: Int)
    }

     fun setOnItemClickListener(listener: OnItemClickListener?) {
        this.mOnItemClickListener = listener
    }

     fun setOnItemLongClickListener(listener: OnItemLongClickListener?) {
        this.mOnItemLongClickListener = listener
    }

}